package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_user_admin;
import cn.com.api.entity.ApiU_discover_cate;
import cn.com.api.service.ApiU_discover_cateService;
import cn.com.api.service.INewFindService;
import cn.com.util.JsonUtil;
import cn.com.util.PageInfo;
import cn.com.util.Utils;





/**
 * 新发现订单后台controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class NewFindController {

	//日志
	private static Logger logger = Logger.getLogger(NewFindController.class);
	
	@Autowired
	private INewFindService newFindService;
	
	@Autowired
	private ApiU_discover_cateService apiU_discover_cateService;
	
	
	
	/**
	 * 新发现活动列表
	 * @return
	 */
	@RequestMapping(value = "new-find-list")
	public ModelAndView new_find_list(HttpServletRequest request, HttpServletResponse response, PageInfo page) throws Exception{
		ModelAndView mv = new ModelAndView("");
		try {
			List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(0);
			String type = request.getParameter("type");
			String rid = request.getParameter("rid");
			String catetype = request.getParameter("ordertype");
			String dd_status = request.getParameter("dd_status");
			String stime = request.getParameter("stime");
			String etime = request.getParameter("etime");
			ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin"); // 获取登录到系统的用户 
			logger.info("==========[NewFindController[new_find_list[查询新发现列表参数[catetype["+catetype+"],type["+type+"],dd_status["+dd_status+"],stime:["+stime+"],etime:["+etime+"]]]==============");
			
			int user_id = admin.getUser_id();
			
			Map<String, Object> cond = new HashMap<String, Object>();
			cond.put("target_id", type);
			String url = "forward:/backstage/new_find_list.jsp";
			
			mv = new ModelAndView(url);
			
			cond.put("type", type);
			cond.put("rid", rid);
			if(!"-1".equals(catetype)){		
				cond.put("catetype", catetype);
			}
			if(!"-1".equals(dd_status)){		
				cond.put("dd_status", dd_status);
			}
			cond.put("stime", stime);
			cond.put("etime", etime);
			cond.put("user_id", user_id);
			List<Map<String, String>> resultList = newFindService.selectByModListPage(cond, page);
			mv.addObject("cates_list", cates);
			mv.addObject("newfind_list", resultList);
			mv.addObject("pageInfo", page);
			logger.info("============[NewFindController[new_find_list[返回结果resultList:["+resultList+"]]]===========");
		} catch (Exception e) {
			logger.error("============[NewFindController[new_find_list[查询新发现列表失败]]]==============",e);
		}
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "newfind-orderinfo-pre")
	public ModelAndView newfind_orderinfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/new_find_orderinfo.jsp");
		try {
			
			String id = (String)request.getParameter("id");
			String user_id = (String)request.getParameter("user_id");
			logger.info("===========[NewFindController[newfind_orderinfo[id:"+id+",user_id:"+user_id+"]]===========");	
			Map<String, Object> newfind = newFindService.getNewFindOrderInfo(id, user_id);		
			mv.addObject("newfind", newfind);
			logger.info("===========NewFindController[newfind_orderinfo[resultList["+newfind+"]===========");	
		} catch (Exception e) {
			logger.error("============[NewFindController[newfind_orderinfo[进入新发现页面失败]]]==============",e);
		}
		return mv;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "newfind-delete")
	public void new_find_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			logger.info("===========[NewFindController[new_find_delete[参数id:"+id+"]]]=============");
			newFindService.delete(id);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		} catch (Exception e) {
			logger.error("===========[NewFindController[new_find_delete[新发现订单删除失败]]]=============", e);
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "删除失败！");
		}
	
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	
}
